package com.aaa.controller.rechargerecord;

import com.aaa.entity.RecharGereCord;
import com.aaa.entity.ResponseDto;
import com.aaa.service.RecharGerecordService;
import com.aaa.service.impl.RecharGerecordServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/AddRechargerecordServlet")
public class AddRechargerecordServlet extends HttpServlet {
    RecharGerecordService recharGerecordService = null;
    @Override
    public void init() throws ServletException {
        recharGerecordService = new RecharGerecordServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer cardId = IntegerUtils.ToInteger(req.getParameter("cardId"));
        Double rechargeAmount = Double.valueOf(req.getParameter("rechargeAmount"));

        Double beforeAmount = Double.valueOf(req.getParameter("beforeAmount"));
        Integer ruleId = IntegerUtils.ToInteger(req.getParameter("ruleId"));
        Integer staffId = IntegerUtils.ToInteger(req.getParameter("staffId"));
        Double afterNum=Double.valueOf(0);
        Double afterAmount=Double.valueOf(0);
        if(ruleId==1&&rechargeAmount>=100) {
             afterNum = (rechargeAmount*5.2) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==2&&rechargeAmount>=200){
             afterNum = (rechargeAmount*7.5) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==3&&rechargeAmount>=100){
             afterNum = (rechargeAmount*0.8) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==4&&rechargeAmount>=10){
             afterNum = (rechargeAmount*0.5) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==5&&rechargeAmount>=1000){
             afterNum = (rechargeAmount*0.5) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==6&&rechargeAmount>=10000){
             afterNum = (rechargeAmount+10000) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==7&&rechargeAmount>=100){
             afterNum = (rechargeAmount*0.2) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else if(ruleId==8&&rechargeAmount>=100){
             afterNum = (rechargeAmount*0.5) + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }else{
             afterNum = rechargeAmount + beforeAmount+rechargeAmount;
             afterAmount = afterNum;
        }
        RecharGereCord recharGereCord = new RecharGereCord();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = simpleDateFormat.format(date);

            recharGereCord.setRuleId(ruleId);
            recharGereCord.setCardId(cardId);
            recharGereCord.setRechargeAmount(rechargeAmount);
            recharGereCord.setBeforeAmount(beforeAmount);
            recharGereCord.setAfterAmount(afterAmount);
            recharGereCord.setCreatedTime(newDate);
            recharGereCord.setStaffId(staffId);

        int num = recharGerecordService.InsertRecharGerecord(recharGereCord);
        ResponseDto responseDto = new ResponseDto();
        if (num > 0){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setMessage("添加成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("添加失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
